﻿/*************************************************************************/
/** Copyright.                                                          **/
/** FileName: RTCNotice.c                                               **/
/** Author: Polarix                                                     **/
/** Version: 1.0.0.0                                                    **/
/** Description: HMI demo for notice box interface and refresh screen.  **/
/*************************************************************************/
//=======================================================================//
//= Include files.                                                      =//
//=======================================================================//
#include "DemoProc.h"
#include "HMI_Engine.h"
#include "SGUI_Basic.h"

extern char time_str[64];
extern LEQ_DATA_EVENT_DATA  LeqData;

//=======================================================================//
//= Macro definition.                                                   =//
//=======================================================================//

//=======================================================================//
//= Static function declaration.                                        =//
//=======================================================================//
static HMI_ENGINE_RESULT    HMI_DemoBasic_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_DemoBasic_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters);
static HMI_ENGINE_RESULT    HMI_DemoBasic_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID);
static HMI_ENGINE_RESULT    HMI_DemoBasic_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID);

//=======================================================================//
//= Static variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_ACTION       s_stDemoBasicPaintActions =     {   NULL,
                                                            HMI_DemoBasic_Prepare,
                                                            HMI_DemoBasic_RefreshScreen,
                                                            HMI_DemoBasic_ProcessEvent,
                                                            HMI_DemoBasic_PostProcess
                                                        };
//=======================================================================//
//= Global variable declaration.                                        =//
//=======================================================================//
HMI_SCREEN_OBJECT       g_stHMIDemo_BasicPaint =        {   HMI_SCREEN_ID_DEMO_BASIC_PAINT,
                                                            &s_stDemoBasicPaintActions
                                                        };

//=======================================================================//
//= Function define.                                                    =//
//=======================================================================//
HMI_ENGINE_RESULT HMI_DemoBasic_Prepare(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    // 20251028 try to cleardisplay the same as pstDeviceIF->fnClear()
    SCREEN_ClearDisplay();
    SGUI_Basic_ResetMask(pstDeviceIF);
    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }
    else
    {
        SGUI_Basic_DrawRectangle1(pstDeviceIF, 0, 0, pstDeviceIF->stSize.iWidth, pstDeviceIF->stSize.iHeight, SGUI_COLOR_BKGCLR, SGUI_COLOR_BKGCLR);
    }
    SGUI_Basic_ResetMask(pstDeviceIF);
    HMI_DemoBasic_RefreshScreen(pstDeviceIF, pstParameters);

    printf("11 HMI_DemoBasic_Prepare\n");
    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_DemoBasic_RefreshScreen(SGUI_SCR_DEV* pstDeviceIF, const void* pstParameters)
{
    printf("2 HMI_DemoBasic_RefreshScreen\n");
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    SGUI_INT                iPaintBkgIdx;

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
   

    // // Paint background.
    // for(iPaintBkgIdx=0; iPaintBkgIdx<pstDeviceIF->stSize.iHeight; iPaintBkgIdx+=5)
    // {
    //     SGUI_Basic_DrawHorizontalLine(pstDeviceIF, 0, pstDeviceIF->stSize.iWidth-1, iPaintBkgIdx, SGUI_COLOR_FRGCLR);
    // }

 

    //printf("aa本地时间（秒级）: %s\n", time_str);
    //printf("leq: a=%d c=%d z=%d\n\n", LeqData.uLeqA, LeqData.uLeqC, LeqData.uLeqZ);

    /*
    // Paint rectangle
    SGUI_Basic_DrawRectangle1(pstDeviceIF, 1, 1, 30, 20, SGUI_COLOR_FRGCLR, SGUI_COLOR_TRANS);
    SGUI_Basic_DrawRectangle1(pstDeviceIF, 1, 22, 30, 20, SGUI_COLOR_FRGCLR, SGUI_COLOR_BKGCLR);
    SGUI_Basic_DrawRectangle1(pstDeviceIF, 1, 43, 30, 20, SGUI_COLOR_FRGCLR, SGUI_COLOR_FRGCLR);

    SGUI_Basic_DrawRectangle1(pstDeviceIF, 37, 1, 5, 20, SGUI_COLOR_FRGCLR, SGUI_COLOR_TRANS);
    SGUI_Basic_DrawRectangle1(pstDeviceIF, 37, 22, 5, 20, SGUI_COLOR_FRGCLR, SGUI_COLOR_BKGCLR);
    SGUI_Basic_DrawRectangle1(pstDeviceIF, 37, 43, 5, 20, SGUI_COLOR_FRGCLR, SGUI_COLOR_FRGCLR);

    // Paint circle
    SGUI_Basic_DrawCircle(pstDeviceIF, 55, 10, 9, SGUI_COLOR_FRGCLR, SGUI_COLOR_TRANS);
    SGUI_Basic_DrawCircle(pstDeviceIF, 55, 31, 9, SGUI_COLOR_FRGCLR, SGUI_COLOR_BKGCLR);
    SGUI_Basic_DrawCircle(pstDeviceIF, 55, 52, 9, SGUI_COLOR_FRGCLR, SGUI_COLOR_FRGCLR);

    // Paint rounded rectangle
    SGUI_Basic_DrawRoundedRectangle(pstDeviceIF, 70, 1, 30, 20, 4, SGUI_COLOR_FRGCLR, SGUI_COLOR_TRANS);
    SGUI_Basic_DrawRoundedRectangle(pstDeviceIF, 70, 22, 30, 20, 4, SGUI_COLOR_FRGCLR, SGUI_COLOR_BKGCLR);
    SGUI_Basic_DrawRoundedRectangle(pstDeviceIF, 70, 43, 30, 20, 4, SGUI_COLOR_FRGCLR, SGUI_COLOR_FRGCLR);

    // Paint rounded rectangle
    SGUI_Basic_DrawRoundedRectangle(pstDeviceIF, 105, 1, 20, 20, 6, SGUI_COLOR_FRGCLR, SGUI_COLOR_TRANS);
    SGUI_Basic_DrawRoundedRectangle(pstDeviceIF, 105, 22, 20, 20, 6, SGUI_COLOR_FRGCLR, SGUI_COLOR_BKGCLR);
    SGUI_Basic_DrawRoundedRectangle(pstDeviceIF, 105, 43, 20, 20, 6, SGUI_COLOR_FRGCLR, SGUI_COLOR_FRGCLR);
    */

    return HMI_RET_NORMAL;
}

HMI_ENGINE_RESULT HMI_DemoBasic_ProcessEvent(SGUI_SCR_DEV* pstDeviceIF, const HMI_EVENT_BASE* pstEvent, SGUI_INT* piActionID)
{
    if(NULL != pstDeviceIF->fnClear)
    {
        pstDeviceIF->fnClear();
    }

    *piActionID = 3;
    // printf("3 Basic_ProcessEvent id=%d\n", *piActionID);
    /*----------------------------------*/
    /* Variable Declaration             */
    /*----------------------------------*/
    HMI_ENGINE_RESULT           eProcessResult;
    SGUI_INT                    iProcessAction;
    KEY_PRESS_EVENT*            pstKeyEvent;
    // LEQ_EVENT*            pstKeyEvent;
    char cLeq[8];

    /*----------------------------------*/
    /* Initialize                       */
    /*----------------------------------*/
    eProcessResult =            HMI_RET_NORMAL;
    iProcessAction =            HMI_DEMO_PROC_NO_ACT;
    pstKeyEvent =               (KEY_PRESS_EVENT*)pstEvent; // (KEY_PRESS_EVENT*)pstEvent;
    //printf("%d b in Base  key=%d  evID=%d\n",__LINE__, pstKeyEvent->Data.uiKeyValue, pstEvent->iID);

    /* SGUI_Common_GetNowTime(SGUI_TIME* pstTime)*/

    // printf("basic process event time=%s index=%d\n" ,time_str);
    //printf("basic process event   date=%d %d\n", pstEvent->iID, pstEvent->iSize);
    //printf("aa本地时间（秒级）: %s\n", time_str);
    //printf("leq: a=%d c=%d z=%d\n\n", LeqData.uLeqA, LeqData.uLeqC, LeqData.uLeqZ);

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, time_str, &SGUI_DEFAULT_FONT_16, 1, 1, 80, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "91", &SGUI_DEFAULT_FONT_16, 91, 1, 127, SGUI_DRAW_NORMAL);

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "SPL", &SGUI_DEFAULT_FONT_16, 1,  30, 30, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "A",   &SGUI_DEFAULT_FONT_16, 51, 14, 30,  SGUI_DRAW_NORMAL);

    if(0 == LeqData.uTimeWeightType)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Fast", &SGUI_DEFAULT_FONT_16, 80, 30, 127, SGUI_DRAW_NORMAL);
    else
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Slow", &SGUI_DEFAULT_FONT_16, 80, 30, 127, SGUI_DRAW_NORMAL);

    // leq 
    switch (LeqData.uTimeWeightType)
    {
    case 0:
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqA / 10);
        //SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "A", &SGUI_DEFAULT_FONT_16, 51, 142, 120, SGUI_DRAW_NORMAL);
        break;
    case 1:
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqC / 10);
        //SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "C", &SGUI_DEFAULT_FONT_16, 51, 142, 120, SGUI_DRAW_NORMAL);
        break;
    case 2:
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqZ / 10);
        //SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "Z", &SGUI_DEFAULT_FONT_16, 51, 142, 120, SGUI_DRAW_NORMAL);
        break;
    default:
        //SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "A", &SGUI_DEFAULT_FONT_16, 51, 142, 120, SGUI_DRAW_NORMAL);
        snprintf(cLeq,sizeof(cLeq), "%.1f", (float)LeqData.uLeqA / 10);
        break;
    }

    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, cLeq, &SGUI_DEFAULT_FONT_16, 30, 60, 127, SGUI_DRAW_NORMAL);
    SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "dB", &SGUI_DEFAULT_FONT_16, 80, 60, 127, SGUI_DRAW_NORMAL);

    // charge status
    // if(1 == LeqData.uChargeStatus)
    //     SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "charging", &SGUI_DEFAULT_FONT_16, 1, 90, 80, SGUI_DRAW_NORMAL);
    // else
    //     SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "", &SGUI_DEFAULT_FONT_16, 1, 90, 80, SGUI_DRAW_NORMAL);

    // over/low
    if(LeqData.uLeqA > 900)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "over", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);
    else if(LeqData.uLeqA < 400)
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "low", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);
    else
        SGUI_Text_DrawMultipleLinesText(pstDeviceIF, "over/low", &SGUI_DEFAULT_FONT_16, 40, 135, 127, SGUI_DRAW_NORMAL);

    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/
    if(EVENT_ID_KEY_PRESS == pstEvent->iID)
    {
        printf("%s:%d EVENT_ID_KEY_PRESS keyvalue=%d\n", __FILE__, __LINE__, pstKeyEvent->Data.uiKeyValue);
        if(313 == pstKeyEvent->Data.uiKeyValue)
        {
            // Event data is invalid.
            eProcessResult = HMI_RET_INVALID_DATA;
        }
        else if(KEY_VALUE_MENU == pstKeyEvent->Data.uiKeyValue)
        {
            // go to main menu
            iProcessAction = HMI_DEMO_PROC_CANCEL;
        }
        else if(KEY_VALUE_PAUSE == pstKeyEvent->Data.uiKeyValue)
        {
            // hold / unhold
            printf("hold/unhold\n");
        }
        else if(KEY_VALUE_RIGHT == pstKeyEvent->Data.uiKeyValue)
        {
            // go to second screen  HMI_SCREEN_ID_MeasureB_PAINT    HMI_SCREEN_ID_DEMO_PAINT_TEXT
             HMI_GoToScreen(HMI_SCREEN_ID_MeasureB_PAINT, NULL);
        }
    }
    if(NULL != piActionID)
    {
        *piActionID = iProcessAction;
    }

    //HMI_DemoBasic_RefreshScreen(pstDeviceIF, NULL);

    return eProcessResult;
}

HMI_ENGINE_RESULT HMI_DemoBasic_PostProcess(SGUI_SCR_DEV* pstDeviceIF, HMI_ENGINE_RESULT eProcResult, SGUI_INT iActionID)
{
    // printf("4 Basic_PostProcess id=%d\n\n", iActionID);
    /*----------------------------------*/
    /* Process                          */
    /*----------------------------------*/

    //printf(" basic  post process  id=%d\n", iActionID);
    if(HMI_PROCESS_SUCCESSFUL(eProcResult))
    {
        if(HMI_DEMO_PROC_CANCEL == iActionID)
        {
            HMI_GoBack(NULL);
        }
    }

    return HMI_RET_NORMAL;
}



